package cn.cnlee.arch.base;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;

import cn.cnlee.arch.util.ClassUtil;
import cn.cnlee.arch.util.Logger;


/**
 * viewModel基类，用范型来约束ViewModel和Repository的对应关系
 */
public class AbsViewModel<T extends AbsRepository> extends AndroidViewModel {

    private final static String TAG = AbsViewModel.class.getName();
    public T mRepository;

    public AbsViewModel(@NonNull Application application) {
        super(application);
        mRepository = ClassUtil.getTypeNewInstance(this, 0);
//        Logger.e(TAG, "初始化ViewModel：" + mRepository);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        if (mRepository != null) {
//            Logger.e(TAG, "UI生命周期结束时，清理CompositeDisposable管理器: " + mRepository);
            mRepository.unDisposable();
        }
    }

}
